package com.walmart.service;



import com.walmart.entity.ShoppingCartItem;
import com.walmart.service.impl.ShoppingCartServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class ShoppingCartService implements ShoppingCartServiceImpl {

    @Override
    public  Double getAmountTotal(List<ShoppingCartItem> items) {
        if (items.size() == 0) {
            return 0d;
        }
        BigDecimal totalAmount = new BigDecimal(0);
        for (ShoppingCartItem item : items) {
            totalAmount = totalAmount.add( item.getAmount());
        }
        return totalAmount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    @Override
    public Double discountAmountTotal(List<ShoppingCartItem> items) {
        BigDecimal totalAmount = BigDecimal.valueOf(getAmountTotal(items));
        BigDecimal discountAmount = totalAmount.divide(BigDecimal.valueOf(100), 0, RoundingMode.DOWN).multiply(BigDecimal.valueOf(10));
        BigDecimal discountAmountTotal = totalAmount.subtract(discountAmount);
        return discountAmountTotal.doubleValue();
    }

}
